﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace NGinnBPM.Lib.Schema
{
    public enum PlaceTypes
    {
        Start,
        End,
        Intermediate
    }

    [Serializable]
    [DataContract]
    public class Place : NetNode
    {
        public Place()
            : base()
        {
        }

        public Place(string id)
            : base()
        {
            Id = id;
        }

        public Place(string id, PlaceTypes typ)
        {
            Id = id; PlaceType = typ;
        }

        [DataMember]
        public PlaceTypes PlaceType { get; set; }

        [DataMember(IsRequired = false, EmitDefaultValue = false)]
        public bool IsImplicit { get; set; }
        

        public override bool Validate(IList<ValidationMessage> msgs)
        {
            int cnt = msgs.Count;
            base.Validate(msgs);
            if (PlaceType == PlaceTypes.Start)
            {
                if (FlowsOut.Count == 0)
                    msgs.Add(new ValidationMessage(true, "Start place without outgoing flows", this));
            }
            if (PlaceType == PlaceTypes.End)
            {
                if (FlowsIn.Count == 0)
                    msgs.Add(new ValidationMessage(false, "End place without inbound flows", this));
            }
            for (int i = cnt; i < msgs.Count; i++)
                if (msgs[i].IsError) return false;
            return true;
        }
    }
}
